package com.mcxx.modules.extremepoverty.controller;

import com.alibaba.fastjson.JSON;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.extremepoverty.service.ExBacklogService;
import com.mcxx.modules.minimumliving.service.ExRecheckService;
import com.mcxx.modules.minimumliving.service.RecheckService;
import com.mcxx.modules.miniSalvation.check.service.write.CheckWriteService;
import com.mcxx.modules.miniSalvation.family.dto.*;
import com.mcxx.modules.miniSalvation.family.entity.DemocraticAppraisalEntity;
import com.mcxx.modules.miniSalvation.family.entity.OrganizationEntity;
import com.mcxx.modules.miniSalvation.family.service.read.AppraisalReadService;
import com.mcxx.modules.miniSalvation.family.service.read.FamilyMemberReadService;
import com.mcxx.modules.miniSalvation.family.service.read.OrganizationReadService;
import com.mcxx.modules.miniSalvation.family.service.write.DemocraticAppraisalWriteService;
import com.mcxx.modules.miniSalvation.family.service.write.FamilyMemberWriteService;
import com.mcxx.modules.miniSalvation.order.dto.*;
import com.mcxx.modules.miniSalvation.order.param.OrderListSearchParam;
import com.mcxx.modules.miniSalvation.order.param.RecheckBatchParam;
import com.mcxx.modules.miniSalvation.order.param.RecheckOrderQueryParam;
import com.mcxx.modules.miniSalvation.order.param.RecheckParam;
import com.mcxx.modules.miniSalvation.order.service.read.InHouseReadService;
import com.mcxx.modules.miniSalvation.order.service.read.OrderReadService;
import com.mcxx.modules.miniSalvation.order.service.read.RecheckBatchReadService;
import com.mcxx.modules.miniSalvation.order.service.write.OrderDetailWriteService;
import com.mcxx.modules.miniSalvation.order.service.write.RecheckBatchWriteService;
import com.mcxx.modules.miniSalvation.salary.service.read.FamilySalaryReadService;
import com.mcxx.modules.miniSalvation.salary.service.write.FamilySalaryWriteService;
import com.mcxx.result.BaseResult;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.mcxx.util.FileExportUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Validated
@Api("复查变更控制类")
@RestService(value = {"extremePoverty/salvation/recheck/v2"})
public class ExSalvationRecheckController extends DefaultRestService {

    private static Logger logger = LoggerFactory.getLogger(ExSalvationRecheckController.class);

    @Autowired
    private OrderReadService orderReadService;
    @Autowired
    private ExRecheckService exRecheckService;
    @Autowired
    private RecheckBatchReadService recheckBatchReadService;
    @Autowired
    private RecheckBatchWriteService recheckBatchWriteService;
    @Autowired
    private FamilyMemberReadService familyMemberReadService;
    @Autowired
    private FamilyMemberWriteService familyMemberWriteService;
    @Autowired
    private ExBacklogService exBacklogService;
    @Autowired
    private InHouseReadService inHouseReadService;
    @Autowired
    private FamilySalaryReadService familySalaryReadService;
    @Autowired
    private OrganizationReadService organizationReadService;
    @Autowired
    private AppraisalReadService appraisalReadService;
    @Autowired
    private DemocraticAppraisalWriteService democraticAppraisalWriteService;
    @Autowired
    private FamilySalaryWriteService familySalaryWriteService;
    @Autowired
    private CheckWriteService checkWriteService;
    @Autowired
    private RecheckService recheckService;
    @Autowired
    private OrderDetailWriteService orderDetailWriteService;


    @PostMapping("writeOffFamilyMember")
    @ApiOperation("注销家庭成员")
    public void writeOffFamilyMember(FamilyMemberDTO dto) {
        logger.info("开始注销家庭成员，参数是：{}", dto);
        familyMemberWriteService.writeOffFamilyMember(dto,getCurUserAccount());
        logger.info("注销家庭成员结束");
    }


    @PostMapping("restoreFamilyMember")
    @ApiOperation("恢复家庭成员")
    public void restoreFamilyMember(FamilyMemberDTO dto) {
        logger.info("开始恢复家庭成员，参数是：{}", dto);
        familyMemberWriteService.restoreFamilyMember(dto,getCurUserAccount());
        logger.info("恢复家庭成员结束");
    }


    @PostMapping("getRecheckFamilyList")
    @ApiOperation("查询复查家庭列表")
    public void getRecheckFamilyList(RecheckOrderQueryParam param, PageParam pageParam) {
        logger.info("开始查询复查家庭列表，参数是：{},{}", JSON.toJSONString(param), JSON.toJSONString(pageParam));
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        Page page = orderReadService.listRecheckFamily(param, pageParam, getCurUserAccount());
        setPageBean(page);
        logger.info("查询复查家庭列表结束，参数是：{}", JSON.toJSONString(page));
    }


    @PostMapping("getRecheckFamilyCount")
    @ApiOperation("查询复查家庭列表(统计信息)")
    public void getRecheckFamilyCount(RecheckOrderQueryParam param) {
        logger.info("开始查询复查家庭列表(统计信息)，参数是：{}", JSON.toJSONString(param));
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        OrderListDTO data = orderReadService.getRecheckFamilyCount(param, getCurUserAccount());
        setData(data);
        logger.info("查询复查家庭列表(统计信息)结束，参数是：{}", JSON.toJSONString(data));
    }


    @PostMapping("getTownRecheckTodoList")
    @ApiOperation("查询乡镇复查待办列表")
    public void getTownRecheckTodoList(RecheckOrderQueryParam param, PageParam pageParam) {
        logger.info("开始查询乡镇复查待办列表，参数是：{},{}", param, pageParam);
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        Page page = orderReadService.getTownRecheckTodoList(param, pageParam, accountVo);
        setPageBean(page);
        logger.info("查询乡镇复查待办列表结束，参数是：{}", page);
    }

    @PostMapping({"downRecheckTodoTownList"})
    public void downRecheckTodoTownList(HttpServletResponse response, @RequestBody RecheckOrderQueryParam param) throws IOException {
        logger.info("导出复查待办对象，参数是：{}", param);
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        ExcelInfo excelInfo = exRecheckService.exportExcelRecheckToDoList(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
        logger.info("导出复查待办对象，结果是：{}", JSON.toJSONString(param));
    }

    @PostMapping("getCountyRecheckTodoList")
    @ApiOperation("查询区县复查待办列表")
    public void getCountyRecheckTodoList(RecheckOrderQueryParam param, PageParam pageParam) {
        logger.info("开始查询区县复查待办列表，参数是：{},{}", param, pageParam);
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        Page page = orderReadService.getCountyRecheckTodoList(param, pageParam, accountVo);
        setPageBean(page);
        logger.info("查询区县复查待办列表结束，参数是：{}", page);
    }

    @PostMapping("downCountyRecheckTodoList")
    @ApiOperation("导出区县复查待办列表")
    public void downCountyRecheckTodoList(HttpServletResponse response, @RequestBody RecheckOrderQueryParam param)throws IOException {
        logger.info("开始导出区县复查待办列表，参数是：{}", param);
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        ExcelInfo excelInfo  = recheckService.exportCountRecheckTodoList(param, accountVo);
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
        logger.info("导出区县复查待办列表结束，参数是：{}", param);
    }

    @PostMapping("addRecheckOrder")
    @ApiOperation("发起复查登记")
    public void addRecheckOrder(@V({"notnull"}) String orderId) {
        logger.info("开始复查登记，参数是：{}", orderId);
        UserAccountVo accountVo = getCurUserAccount();
        try {
            String newOrderId = exRecheckService.addRecheckOrder(orderId, accountVo);
            setData(newOrderId);
            logger.info("复查登记结束，参数是：{}", newOrderId);
        } catch (BizException e) {
            setError(e.getErrCode(), e.getErrorMessage());
            logger.error(e.getErrorMessage());
        }
    }

















    @PostMapping("backSalvationRequest")
    @ApiOperation("撤销回退救助申请")
    public void backSalvationRequest(@V("notnull") String orderId, @V("notnull") String revokeReason) {
        logger.info("撤销回退救助申请，参数是：{}", orderId);
        exRecheckService.cancelSalvationRequestTown(orderId,revokeReason,getCurUserAccount());
        setData(true);
        logger.info("撤销回退救助申请，结果是：{}", true);
    }

    @PostMapping("cancelSalvationRequest")
    @ApiOperation("撤销救助申请")
    public void cancelSalvationRequest(@RequestPart List<String> orderIdList) {
        logger.info("撤销救助申请，参数:{}", orderIdList);
        Map<String, List<String>> stringListMap  = exRecheckService.cancelSalvationRequestTowns(orderIdList, getCurUserAccount());
        if(stringListMap.isEmpty()){
            setData("success");
        }else{
            setData("用户【"+String.join("," , stringListMap.get("noExistState"))+"】状态无法删除");
        }
        logger.info("撤销救助申请，结果是：{}", stringListMap);
    }



    @GetMapping("getFamilyMemberList")
    @ApiOperation("根据orderId查询家庭成员列表")
    public void getFamilyMemberList(@V({"notnull"}) String orderId) {
        logger.info("根据orderId查询家庭成员列表，参数：{}", orderId);
        List<FamilyMemberDTO> memberList = familyMemberReadService.listMemberByOrderId(orderId);
        setData(memberList);
        logger.info("根据orderId查询家庭成员列表完成，结果：{}", JSON.toJSONString(memberList));
    }



    @PostMapping("sendCheck")
    @ApiOperation("发起核对")
    public void sendCheck(@V("notnull") @RequestPart List<CheckDTO> checkDTOList){
        logger.info("发起核对，参数是：{}", checkDTOList);
        Map<String, List<String>> rstMap = exBacklogService.sendCheck(checkDTOList, getCurUserAccount(), Constant.BusinessCode.TEKUN,Constant.CheckType.RECHECK);
        setData(this.packageSendCheckRst(rstMap));
        logger.info("发起核对，结果是：{}", rstMap);
    }

    @PostMapping("completeCheck")
    @ApiOperation("完成核对，修改状态")
    public void completeCheck(@RequestPart List<String> orderIdList){
        logger.info("完成核对，修改状态：{}", orderIdList);
        checkWriteService.completeCheck(orderIdList);
        logger.info("完成核对，修改状态：结果：{}", true);
    }


    private Map<String, String> packageSendCheckRst( Map<String, List<String>> rstMsgMap){
        Map<String, String> rstMap = new HashMap<>();
        StringBuffer rstMsg = new StringBuffer();
        if(rstMsgMap.containsKey("noExistCert") == false && rstMsgMap.containsKey("canNotSendCheck") == false
                && rstMsgMap.containsKey("masterNumError") == false && rstMsgMap.containsKey("noReport") == false
                && rstMsgMap.containsKey("noExistsAssests") == false
                && rstMsgMap.containsKey("memberInfoError") == false
                && rstMsgMap.containsKey("noBackUpOrder") == false
                && rstMsgMap.containsKey("noIdCardSupport") == false
                && rstMsgMap.containsKey("memberNoExistCert") == false
                && rstMsgMap.containsKey("success") ){
            rstMap.put("code", "1");
            rstMap.put("msg", rstMsgMap.get("checkMsg").get(0));
            return rstMap;
        }else if(rstMsgMap.containsKey("noExistCert") == false && rstMsgMap.containsKey("canNotSendCheck") == false
                && rstMsgMap.containsKey("masterNumError") == false && rstMsgMap.containsKey("noReport") == false
                && rstMsgMap.containsKey("noExistsAssests") == false
                && rstMsgMap.containsKey("memberInfoError") == false
                && rstMsgMap.containsKey("noBackUpOrder") == false
                && rstMsgMap.containsKey("noIdCardSupport") == false
                && rstMsgMap.containsKey("memberNoExistCert") == false
                && rstMsgMap.containsKey("fail")){
            rstMap.put("code", "-1");
            rstMap.put("msg", "操作失败");
            return rstMap;
        }
        if(rstMsgMap.containsKey("masterNumError")){
            rstMsg.append("申请人身份证为：【").append(String.join(",",rstMsgMap.get("masterNumError"))).append("】户主信息错误，不能发起核对；\n");
        }
        if(rstMsgMap.containsKey("memberInfoError")){
            rstMsg.append("【").append(String.join(",",rstMsgMap.get("memberInfoError"))).append("】家庭存在未完善的成员信息，不能发起核对；\n");
        }
        if(rstMsgMap.containsKey("canNotSendCheck")){
            rstMsg.append("家庭【").append(String.join(",",rstMsgMap.get("canNotSendCheck"))).append("】核对中，不能发起核对；\n");
        }
        if(rstMsgMap.containsKey("noExistsAssests")){
            rstMsg.append("【").append(String.join(",",rstMsgMap.get("noExistsAssests"))).append("】家庭收入抵扣项未完善，不能发起核对；\n");
        }
        if(rstMsgMap.containsKey("noReport")){
            rstMsg.append("【").append(String.join(",",rstMsgMap.get("noReport"))).append("】已发起核对且无核对报告返回，不能继续发起核对；");
        }
        if(rstMsgMap.containsKey("noExistCert")){
            rstMsg.append("【").append(String.join(",",rstMsgMap.get("noExistCert"))).append("】不存在经济核对材料/户口本，不能发起核对；\n");
        }
        if(rstMsgMap.containsKey("noBackUpOrder")){
            rstMsg.append("【").append(String.join(",",rstMsgMap.get("noBackUpOrder"))).append("】没有生成诚信申请信息，不能发起核对；\n");
        }
        if(rstMsgMap.containsKey("memberNoExistCert")){
            rstMsg.append("【").append(String.join(",",rstMsgMap.get("memberNoExistCert"))).append("】家庭存在成员没有上传身份证，不能发起核对；\n");
        }
        if(rstMsgMap.containsKey("noIdCardSupport")){
            rstMsg.append("【").append(String.join(",",rstMsgMap.get("noIdCardSupport"))).append("】赡(抚)养人没有上传身份证，不能发起核对；\n");
        }
        if(rstMsgMap.containsKey("noAuthorSupport")){
            rstMsg.append("【").append(String.join(",",rstMsgMap.get("noAuthorSupport"))).append("】赡(抚)养人没有上传授权书，不能发起核对；\n");
        }
        if(rstMsgMap.containsKey("success")){
            rstMsg.append("【").append(String.join(",",rstMsgMap.get("success"))).append("】发起核对；\n");
        }
        if(rstMsgMap.containsKey("noAuthorCert")){
            rstMsg.append("户主身份证为：【").append(String.join(",",rstMsgMap.get("noAuthorCert"))).append("】没有家庭经济状况核对授权书,不能发起复核；\n");
        }
        if(rstMsgMap.containsKey("idcardInBusiness")){
            rstMsg.append("户主身份证为：【").append(String.join(",",rstMsgMap.get("idcardInBusiness"))).append("】正在业务办理中,不能发起复核；\n");
        }
        if(rstMsgMap.containsKey("masterError")){
            rstMsg.append("户主身份证为：【").append(String.join(",",rstMsgMap.get("masterError"))).append("】户主数量或者状态不对,不能发起复核；\n");
        }
        if(rstMsgMap.containsKey("checkMsg")){
            rstMsg.append(rstMsgMap.get("checkMsg").get(0));
        }
        rstMap.put("code", "-1");
        rstMap.put("msg", rstMsg.toString());
        return rstMap;
    }


    @PostMapping("getInHouseRecord")
    @ApiOperation("根据orderId查询入户调查记录（乡镇/区县）")
    public void getInHouseRecord(@V("notnull") String orderId, @V("notnull") String areaLevel) {
        logger.info("根据orderId查询入户调查记录，参数是：{}", orderId);
        OrderInHouseDTO orderInHouseDTO = inHouseReadService.queryNewByOrderId(orderId, areaLevel);
        setData(orderInHouseDTO);
        logger.info("根据orderId查询入户调查记录，结果是：{}", orderInHouseDTO);
    }


    @PostMapping("saveInHouseRecord")
    @ApiOperation("保存入户调查信息")
    public void saveInHouseRecord(OrderInHouseDTO orderInHouse, @RequestPart List<String> orderCertIdList) {
        logger.info("保存入户调查信息，参数是：{}", orderInHouse);
        String inHouseId = exBacklogService.saveInHouseRecord(orderInHouse, orderCertIdList, getCurUserAccount());
        setData(inHouseId);
        logger.info("保存入户调查信息，结果是：{}", inHouseId);
    }


    @PostMapping("modifyInHouseRecord")
    @ApiOperation("更新入户调查信息")
    public void modifyInHouseRecord(@V("notnull") OrderInHouseDTO orderInHouse,
                                    @V("notnull") @RequestPart List<String> orderCertIdList) {
        logger.info("更新入户调查信息，参数是：{}", orderInHouse);
        exBacklogService.modifyInHouseRecord(orderInHouse, orderCertIdList, getCurUserAccount());
        setData(true);
        logger.info("保存入户调查信息，结果是：{}", true);
    }


    @PostMapping("saveFamilySalaryInfo")
    @ApiOperation("计算家庭救助待遇")
    public void saveFamilySalaryInfo(@RequestBody  @V("notnull") ExOrderSalaryDTO orderSalary) {

        logger.info("计算家庭救助待遇，参数是orderSalary：{}", orderSalary);
        orderSalary.setBusinessType(Constant.BusinessCode.TEKUN);
        BaseResult rstMsg = familySalaryWriteService.salaryDT(null,orderSalary, getCurUserAccount(),orderSalary.getType() == null ? 2 : 1,Constant.BusinessCode.TEKUN);
        setData(rstMsg);
        logger.info("计算家庭救助待遇，结果是：{}", rstMsg);
    }


    @PostMapping("getFamilySalaryInfo")
    @ApiOperation("得到家庭救助待遇")
    public void getFamilySalaryInfo(@V({"notnull"}) String orderId) {
        logger.info("得到家庭救助待遇，参数是orderId：{}", orderId);
        Map<String, Object> rstMap = familySalaryReadService.getRecheckFamilySalaryInfo(orderId,null);
        setData("data", rstMap);
        logger.info("得到家庭救助待遇，结果是：{}", rstMap);
    }


    @PostMapping("getFamilySalaryInfoCounty")
    @ApiOperation("得到家庭救助待遇（区县）")
    public void getFamilySalaryInfoCounty(String orderId){
        logger.info("得到家庭救助待遇（区县），参数是orderId：{}", orderId);
        Map<String, Object> rstMap = familySalaryReadService.getRecheckFamilySalaryInfo(orderId,null);
        setData(rstMap);
        logger.info("得到家庭救助待遇（区县），结果是：{}", rstMap);
    }

    @PostMapping("getInforToAudit")
    @ApiOperation("查询信息去审核")
    public void getInforToAudit(@V("notnull") String orderId){
        logger.info("审核前查询信息,参数是:{},{}",orderId);
        Map<String,Object> resMap= exBacklogService.getInfoBeforeAudit(orderId);
        setData("data",resMap);
        logger.info("审核前查询信息，结果是{}",resMap);

    }


    @PostMapping("getOldAgeHomes")
    @ApiOperation("根据地区读取相应的机构")
    public void getOldAgeHomes(){
        logger.info("根据地区读取相应的机构");
        String areaCode = getCurUserAccount().getAreaCode().substring(0,6);
        List<OrganizationEntity> orgList = organizationReadService.getOldAgeHomesByAreaCode(areaCode);
        setData(orgList);
        logger.info("根据地区读取相应的机构,结果是：{}", orgList);
    }


    @PostMapping("getSalvationAudit")
    @ApiOperation("查询审核信息")
    public void getSalvationAudit(@V("notnull") String orderId) {
        logger.info("查询审核信息，参数是：{}", orderId);
        BaseResult<OrderDetailDTO> rstMsg = exBacklogService.getOrderDetail(orderId, Constant.CheckDatailStatus.he);
        setData(rstMsg);
        logger.info("查询审核信息，结果是：{}", rstMsg);
    }


    @PostMapping("confirmFamilySalaryInfo")
    @ApiOperation("确认家庭救助待遇")
    public void confirmFamilySalaryInfo(@RequestPart ExOrderSalaryDTO orderSalary,
                                        @RequestPart Map<String, String> memberMap) {
        logger.info("计算家庭救助待遇，参数是orderSalary：{},familyMemberLMap:{}", orderSalary, memberMap);
        BaseResult<Boolean> rstMsg = exBacklogService.confirmFamilySalaryInfo(orderSalary, memberMap,
                getCurUserAccount());
        setData(rstMsg);
        logger.info("计算家庭救助待遇，结果是：{}", rstMsg);
    }


    @PostMapping("saveSalvationAudit")
    @ApiOperation("保存审核信息")
    public void saveSalvationAudit(OrderReviewDTO orderReviewDTO) {
        logger.info("保存审核信息，参数是：{}", orderReviewDTO);
        exBacklogService.saveSalvationAudit(orderReviewDTO, getCurUserAccount());
        setData(true);
        logger.info("保存审核信息，结果是：{}", orderReviewDTO);
    }


    @PostMapping("submitSalvationRequest")
    @ApiOperation("上报救助申请")
    public void submitSalvationRequest(OrderReviewDTO orderReviewDTO) {
        logger.info("上报救助申请，参数是：{}", orderReviewDTO);
        int rstFlag = exBacklogService.submitSalvationRequest(orderReviewDTO, getCurUserAccount());
        setData(true);
        logger.info("上报救助申请，结果是：{}", rstFlag);
    }


    @PostMapping("modifySalvationAudit")
    @ApiOperation("编辑审核信息")
    public void modifySalvationAudit(OrderReviewDTO orderReviewDTO) {
        logger.info("编辑审核信息，参数是：{}", orderReviewDTO);
        exBacklogService.saveSalvationAudit(orderReviewDTO, getCurUserAccount());
        setData(true);
        logger.info("编辑审核信息，结果是：{}", true);
    }


    @PostMapping("isCanInHouse")
    @ApiOperation("判读是否可以发起入户调查")
    public void isCanInHouse(String orderId) {




//        HandleType handleType = new HandleType(BusinessConstant.COMMON, BusinessConstant.BIZ_RECHECK,




        logger.info("判读是否可以发起入户调查，参数orderId是：{}", orderId);
        exBacklogService.isCanInHouseRecheck(orderId, getCurUserAccount());
        setData(true);
        logger.info("判读是否可以发起入户调查，结果：{}", true);
    }


    @PostMapping("isCanCalcFamilySalary")
    @ApiOperation("判断是否可以进行待遇计算")
    public void isCanCalcFamilySalary(String orderId) {
        logger.info("判断是否可以进行待遇计算，参数，orderId:{}", orderId);
        int rst = exBacklogService.isCanCalcFamilySalary(orderId);
        setData(rst);
        logger.info("判断是否可以进行待遇计算，结果是:{}", rst);
    }





    @PostMapping("isCanAudit")
    @ApiOperation("判断是否可以进行审核")
    public void isCanAudit(@V("notnull") String orderId,@V("notnull") String familyId){
        logger.info("判断是否可以进行审核，参数，orderId:{},familyId", orderId,familyId);
        exBacklogService.isCanAuditRecheck(orderId,familyId,getCurUserAccount());
        setData(true);
        logger.info("判断是否可以进行审核，结果是:{}", true);
    }


    @PostMapping("getSalvationApprove")
    @ApiOperation("查询审批信息")
    public void getSalvationApprove(@V("notnull") String orderId) {
        logger.info("查询审批信息，参数是：{}", orderId);
        BaseResult<OrderApproveDTO>  rstMsg = exBacklogService.getSalvationApprove(orderId);
        setData(rstMsg);
        logger.info("查询审批信息，结果是：{}", rstMsg);
    }


    @PostMapping("approveSalvationRequest")
    @ApiOperation("审批复查业务")
    public void approveSalvationRequest(OrderApproveDTO orderApproveDTO) {
        logger.info("审批复查业务，参数是：{}", JSON.toJSONString(orderApproveDTO));
        exRecheckService.approveRecheckOrder(orderApproveDTO, getCurUserAccount());
        logger.info("审批复查业务完成");
    }





    @PostMapping("listBatchRecheck")
    @ApiOperation("查询批量复核列表")
    public void listBatchRecheck(RecheckBatchParam param, PageParam pageParam) {
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        logger.info("查询批量复核列表，参数：{}，分页参数：{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        Page page =  recheckBatchReadService.listBatchRecheck(param,pageParam,getCurUserAccount());
        setPageBean(page);
        logger.info("查询批量复核列表完成");
    }

    @PostMapping("sendBatchCheck")
    @ApiOperation("复核发起核对")
    public void sendBatchCheck(@V("notnull") @RequestPart List<CheckDTO> checkDTOList){
        logger.info("复核发起核对，参数是：{}", checkDTOList);
        Map<String, List<String>> rstMsgMap = exBacklogService.sendBatchCheck(checkDTOList, getCurUserAccount(), Constant.BusinessCode.TEKUN,Constant.CheckType.BATCH_RECHECK);
        Map<String, String> rstMap = this.packageSendCheckRst(rstMsgMap);
        setData("code", rstMap.get("code"));
        setData("msg", rstMap.get("msg"));
        logger.info("复核发起核对，结果是：{}", rstMap);
    }

    @PostMapping("downloadBatchRecheck")
    @ApiOperation("导出批量复核列表")
    public void downloadBatchRecheck(HttpServletResponse response,@RequestBody RecheckBatchParam param) throws IOException{
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        logger.info("导出批量复核列表，参数：{}", JSON.toJSONString(param));
        ExcelInfo excelInfo = recheckBatchWriteService.exportExcelRecheckResultList(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
        logger.info("导出批量复核列表完成");
    }

    @PostMapping("queryRecheckFamily")
    @ApiOperation("查询不在某个批次的待复查变更对象列表")
    public void queryReCheckFamily(OrderListSearchParam param, PageParam pageParam){
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        param.setState(Constant.BizState.APPROVE_YES);
        logger.info("查询不在某个批次的待复查变更对象列表，参数：{}，分页：{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        Page page = orderReadService.queryRecheckFamily(param,pageParam,getCurUserAccount());
        setPageBean(page);
        logger.info("查询不在某个批次的待复查变更对象列表完成，结果：{}", JSON.toJSONString(page));
    }


    @PostMapping("querySelectRecheckFamily")
    @ApiOperation("查询某个批次已选择的复查对象列表")
    public void querySelectReCheckFamily(OrderListSearchParam param, PageParam pageParam){
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        param.setState(Constant.BizState.APPROVE_YES);
        logger.info("查询某个批次已选择的复查对象列表，参数：{}，分页：{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        Page page = orderReadService.querySelectRecheckFamily(param,pageParam,getCurUserAccount());
        setPageBean(page);
        logger.info("查询某个批次已选择的复查对象列表完成，结果：{}", JSON.toJSONString(page));
    }


    @PostMapping("addRecheckOrderNo")
    @ApiOperation("添加复查对象")
    public void addRecheckOrderNo(@RequestPart  String[] orderIdList , String batchNo){
        logger.info("添加复查对象，参数是：{},{}", orderIdList,batchNo);
        if(orderIdList != null && orderIdList.length < 1 ){
            setError("所属家庭不能为空");
        }
        if(StringUtils.isEmpty(batchNo)){
            setError("批量编码不能为空");
        }
        BaseResult<Boolean> res =  exRecheckService.addRecheckOrderNo(orderIdList,batchNo,getCurUserAccount());
        setData(res.getData());
        logger.info("添加复查对象完成，结果：{}", res.getData());
    }



    @PostMapping("deleteRecheckMemer")
    @ApiOperation("删除批量复查中的复查对象")
    public void deleteRecheckMemer(@RequestPart String[] checkedList ,@RequestPart String[] orderIdList , String checkBatchNo){
        logger.info("删除批量复查中的复查对象，参数：checkedList：{}，orderIdList：{}，checkBatchNo：{}", JSON.toJSONString(checkedList),JSON.toJSONString(orderIdList),checkBatchNo);

        if(orderIdList == null && orderIdList.length < 1 ){
            setError(FailureCode.ERR_30005.getMessage());
        }
        if(StringUtils.isEmpty(checkBatchNo)){
            setError(FailureCode.ERR_30004.getMessage());
        }
        if(StringUtils.isEmpty(checkBatchNo)){
            setError(FailureCode.ERR_30005.getMessage());
        }
        String  state = Constant.CheckState.SAVE;
        String type = Constant.BusiCode.RECHECK_BATCH;
        BaseResult<Boolean>  res =  exRecheckService.deleteRechekOrderNo(orderIdList,checkedList,checkBatchNo,state,type);
        setData(res.getData());
    }


    @SneakyThrows
    @PostMapping("getRecheckResultList")
    @ApiOperation("复查结果")
    public void getRecheckedResult(RecheckParam queryParam, PageParam pageParam) {
        logger.info("查询复查结果列表，查询参数：{},分页参数：{}", JSON.toJSONString(queryParam), JSON.toJSONString(pageParam));
        queryParam.setBusinessType(Constant.BusinessCode.TEKUN);
        queryParam.setOperType(Constant.OrderType.RECHECK);
        Page page = orderReadService.listRecheckResult(queryParam, pageParam, getCurUserAccount());
        setPageBean(page);
        logger.info("查询复查结果列表，参数为{}", JSON.toJSONString(page));
    }



    @PostMapping({"downRecheckResultList"})
    public void downRecheckResultList(HttpServletResponse response, @RequestBody RecheckParam param) throws IOException {
        logger.info("导出复查结果对象，参数是：{}", param);
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        ExcelInfo excelInfo = exRecheckService.exportExcelRecheckResultList(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
        logger.info("导出复查结果对象，结果是：{}", JSON.toJSONString(param));
    }

    @PostMapping("tempSaveRecheckInHouse")
    @ApiOperation("暂存入户调查")
    public void tempSaveRecheckInHouse(ExtremePovertyApplyDTO applyDto){
        logger.info("暂存入户调查，参数是：{}", applyDto);
        ApplyRtnDTO applyRtnDTO = exRecheckService.tempSaveRecheckInHouse(applyDto, getCurUserAccount());
        setData(applyRtnDTO);
        logger.info("暂存入户调查，结果是：{}", JSON.toJSONString(applyDto));
    }


    @PostMapping("completeRecheckInHouse")
    @ApiOperation("结束入户调查")
    public void completeRecheckInHouse(ExtremePovertyApplyDTO applyDto){
        logger.info("结束入户调查，参数是：{}", applyDto);
        ApplyRtnDTO applyRtnDTO = exRecheckService.completeRecheckInHouse(applyDto, getCurUserAccount());
        setData(applyRtnDTO);
        logger.info("结束入户调查，结果是：{}", JSON.toJSONString(applyDto));
    }


    @PostMapping("changeFamnilySave")
    @ApiOperation("修改保障对象")
    public void changeFamnilySave(String orderId,String familyId,String memberId,String type,String isMaster){
        logger.info("修改保障对象，参数是orderSalary：{}", memberId);
        int rstMsg = exBacklogService.chengeMemberSaveObject(orderId,familyId,memberId,type,isMaster);
        setData(rstMsg);
        logger.info("修改保障对象，结果是：{}", rstMsg);
    }


    @PostMapping("submitSalvationAuditBatch")
    @ApiOperation("批量上报")
    public void submitSalvationAuditBatch(@RequestPart List<String> orderIdList) {
        logger.info("批量上报，参数是：{}", orderIdList);
        Map<String, List<String>> stringListMap = exBacklogService.exceBatchReport(orderIdList,getCurUserAccount());
        if(stringListMap.isEmpty()){
            setData("success");
        }else{
            setData("用户【"+String.join("," , stringListMap.get("noExistState"))+"】状态不是待上报，不能操作");
        }
        logger.info("批量上报，结果是：{}", stringListMap);
    }

    @PostMapping("isCanDemocraticAppraisal")
    @ApiOperation("判断能否进行民主评议")
    public void isCanDemocraticAppraisal(@V("notnull") String orderId){
        logger.info("判断能否进行民主评议，参数orderId是：{}", orderId);
        exBacklogService.reCheckCanDemocraticAppraisal(orderId, getCurUserAccount());
        setData(true);
        logger.info("判断能否进行民主评议，结果是：{}", true);
    }


    @PostMapping("isCanInHouseCounty")
    @ApiOperation("区县-判读是否可以发起入户调查")
    public void isCanInHouseCounty(String orderId){
        logger.info("区县-判读是否可以发起入户调查，参数orderId是：{}", orderId);

        UserAccountVo userAccountVo = getCurUserAccount();
        exBacklogService.isCanInHouseCounty(orderId,userAccountVo);
        logger.info("区县-判读是否可以发起入户调查，结果：{}", 1);
        setData(1);
    }


    @PostMapping("confirmInhouseRecordCounty")
    public void confirmInhouseRecordCounty(@V("notnull") OrderInHouseDTO orderInHouse, @V("notnull") @RequestPart List<String> orderCertIdList){
        logger.info("区县结束入户调查信息，参数是：{}", orderInHouse);
        String inhouseId = exBacklogService.confirmInhouseRecordCounty(orderInHouse,orderCertIdList, getCurUserAccount());
        setData(inhouseId);
        logger.info("区县结束入户调查信息，结果是：{}", true);
    }


    @PostMapping("queryReportDetail")
    @ApiOperation("区县-获取乡镇上报信息")
    public void queryReportDetail(String orderId){
        logger.info("区县-获取乡镇上报信，参数orderId是：{}", orderId);
        Map<String, Object> rstMap = exBacklogService.queryReportDetail(orderId);
        setData("data", rstMap);
        logger.info("区县-获取乡镇上报信息，结果：{}", rstMap);
    }



    @PostMapping("isCanAuditCounty")
    @ApiOperation("区县-判断是否可以审批")
    public void isCanAuditCounty(String orderId){
        logger.info("区县-判断是否可以审批，参数orderId是：{}",orderId);
        exBacklogService.isCanAuditCounty(orderId, getCurUserAccount());
        logger.info("区县-判断是否可以审批，结果：{}", true);
        setData(true);
    }

    @PostMapping("saveApproveSalvation")
    @ApiOperation("暂存审批信息")
    public void saveApproveSalvation(OrderApproveDTO orderApproveDTO){
        logger.info("暂存审批信息，参数是：{}", orderApproveDTO);
        exBacklogService.saveApproveSalvation(orderApproveDTO, getCurUserAccount());
        setData(true);
        logger.info("暂存审批信息，结果是：{}", true);
    }

    @GetMapping("getDemocracyAppraisal")
    @ApiOperation("根据orderId查询民主评议信息")
    public void getDemocracyAppraisal(@V("notnull") String orderId) {
        logger.info("根据orderId查询民主评议信息，参数，orderId:{}",orderId);
        DemocraticAppraisalDTO dto = appraisalReadService.getAppraisalByOrderIdForNew(orderId);
        setData(dto);
        logger.info("根据orderId查询民主评议信息，结果是:{}",dto);
    }

    @PostMapping("addDemocraticAppraisal")
    @ApiOperation("添加民主评议信息")
    public void addDemocraticAppraisal(@Valid DemocraticAppraisalEntity entity, @RequestPart List<String> orderCertIdList) {
        logger.info("添加民主评议信息，参数，entity:{}", entity);
        String appraisalId = exBacklogService.addDemocraticAppraisal(entity, orderCertIdList, getCurUserAccount());
        setData(appraisalId);
        logger.info("添加民主评议信息，结果是:{}", appraisalId);
    }

    @PostMapping("modifyDemocraticAppraisal")
    @ApiOperation("修改民主评议信息")
    public void modifyDemocraticAppraisal(@Valid DemocraticAppraisalEntity entity, @RequestPart List<String> orderCertIdList) {
        logger.info("编辑民主评议信息，参数是：{}", entity);
        democraticAppraisalWriteService.modifyDemocraticAppraisal(entity,orderCertIdList);
        setData(true);
        logger.info("编辑民主评议信息，结果是：{}", true);
    }

    @PostMapping("verifyHouseholdCancelFundPay")
    @ApiOperation("整户停保名册验证")
    public void verifyFundPay(OrderApproveDTO orderApproveDTO){
        logger.info("整户停保验证，参数是：{}", JSON.toJSONString(orderApproveDTO));
        BaseResult<Map> result = exBacklogService.verifyHouseholdCancelFundPay(orderApproveDTO, getCurUserAccount());
        setData(result);
        logger.info("整户停保名册验证");
    }

    @PostMapping("approveHouseholdCancel")
    @ApiOperation("审批整户停保")
    public void approveHouseholdCancel(OrderApproveDTO orderApproveDTO){
        logger.info("审批整户停保，参数是：{}", JSON.toJSONString(orderApproveDTO));
        exRecheckService.approveHouseholdCancel(orderApproveDTO, getCurUserAccount());
        logger.info("审批整户停保完成");
    }

    @PostMapping("repealCheck")
    @ApiOperation("终止核对")
    public void repealCheck(@V("notnull") String checkId){
        logger.info("终止核对,参数是{}",checkId);
        int rst = recheckService.repealCheck(checkId,getCurUserAccount());
        setData(rst);
        logger.info("终止核对,结果是：{}",rst);
    }


    @PostMapping("getApplyDateByOrderId")
    @ApiOperation("查询sourceId的审核日期")
    public void getApplyDateByOrderId(String orderId){
        logger.info("查询sourceId的审核日期，参数是orderId：{}", orderId);
        String applyDate = familySalaryReadService.getApplyDateByOrderId(orderId);
        setData(applyDate);
        logger.info("查询sourceId的审核日期，结果是：{}", applyDate);
    }


    @PostMapping("backRecehckResult")
    @ApiOperation("区县复查审核撤销")
    public void backRecehckResult(@RequestParam("orderId")String orderId, @RequestParam("revokeReason") String revokeReason) {
        logger.info("区县复查审核撤销，参数是：{},申请原因：{}", orderId,revokeReason);
        exRecheckService.backSalvationFinished(orderId,revokeReason,getCurUserAccount());
        setData(true);
        logger.info("区县复查审核撤销，结果是：{}", true);
    }


    @PostMapping("isCanUploadCheckFile")
    @ApiOperation("是否可以上传核对文件")
    public void isCanUploadCheckFile(String familyId,String orderId){
        logger.info("是否可以上传核对文件,参数是{},{}",familyId,orderId);
        exBacklogService.isCanUploadCheckFile(familyId,orderId);
        logger.info("是否可以上传核对文件：{}",1);
    }

    @PostMapping("uploadCheckFile")
    @ApiOperation("上传核对文件")
    public void uploadCheckFile(@RequestParam("file") MultipartFile[] file, String orderId, String reason){
        logger.info("上传核对文件,参数是{}",orderId);
        int rst = exBacklogService.uploadCheckFile(file,orderId,reason,getCurUserAccount().getAreaCode());
        logger.info("上传核对文件,结果是：{}",rst);
    }
}
